package piccolo;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import xml.XMLWriter;
import dbConnections.MsAccessConnection;
import dbStruct.DatabaseStructure;
import filefilter.MDBFileFilter;
import filefilter.XMLFileFilter;

public class Menu extends JMenuBar implements ActionListener{
	private static final long serialVersionUID = 5802434924071075024L;
	private ReadXML xml;
	
	public Menu()
	{
		super();
		setMenu();
	}
	
	private void setMenu()
	{
		setMenuFile();
		setMenuView();
		setMenuTool();
		
	}
	
	private void setMenuFile()
	{
		JMenuItem item;
		JMenu menu;
		JMenu subMenu;
		
		menu = new JMenu("FILE");
		menu.setMnemonic(KeyEvent.VK_M);
		this.add(menu);
		
		subMenu = new JMenu();
		subMenu.setText("NUOVO");
		subMenu.setMnemonic(KeyEvent.VK_N);
		subMenu.addActionListener(this);
			//submenu
			item = new JMenuItem();
			item = new JMenuItem();
			item.setText("MS ACCESS");
			item.setMnemonic(KeyEvent.VK_A);
			item.addActionListener(this);
			subMenu.add(item);
			
			item = new JMenuItem();
			item.setText("MYSQL");
			item.setMnemonic(KeyEvent.VK_M);
			item.addActionListener(this);
			subMenu.add(item);
		menu.add(subMenu);
		
		
		item = new JMenuItem();
		item.setText("APRI");
		item.setMnemonic(KeyEvent.VK_A);
		item.addActionListener(this);
		menu.add(item);
		
		item = new JMenuItem();
		item.setText("IMPORTA");
		item.setMnemonic(KeyEvent.VK_I);
		item.addActionListener(this);
		menu.add(item);
		
		item = new JMenuItem();
		item.setText("SALVA");
		item.setMnemonic(KeyEvent.VK_S);
		item.addActionListener(this);
		menu.add(item);
		
		menu.addSeparator();
		
		item = new JMenuItem();
		item.setText("STAMPA");
		item.setMnemonic(KeyEvent.VK_P);
		item.addActionListener(this);
		menu.add(item);
		
		menu.addSeparator();
		
		item = new JMenuItem();
		item.setText("ESCI");
		item.setMnemonic(KeyEvent.VK_N);
		item.addActionListener(this);
		menu.add(item);
	}

	private void setMenuView()
	{
		JMenuItem item;
		JMenu menu;
		
		menu = new JMenu("VISTA");
		menu.setMnemonic(KeyEvent.VK_V);
		this.add(menu);
		
		item = new JMenuItem();
		item.setText("RESET");
		item.setMnemonic(KeyEvent.VK_R);
		item.addActionListener(this);
		menu.add(item);
				  
		item = new JMenuItem();
		item.setText("APRI");
		item.setMnemonic(KeyEvent.VK_A);
		item.addActionListener(this);
		menu.add(item);
		
		item = new JMenuItem();
		item.setText("SALVA");
		item.setMnemonic(KeyEvent.VK_S);
		item.addActionListener(this);
		menu.add(item);
		
		menu.addSeparator();
		
		item = new JMenuItem();
		item.setText("STAMPA");
		item.setMnemonic(KeyEvent.VK_P);
		item.addActionListener(this);
		menu.add(item);
		
		menu.addSeparator();
		
		item = new JMenuItem();
		item.setText("ESCI");
		item.setMnemonic(KeyEvent.VK_N);
		item.addActionListener(this);
		menu.add(item);
	}
	
	private void setMenuTool()
	{
		JMenuItem item;
		JMenu menu;
		
		menu = new JMenu("STRUMENTI");
		menu.setMnemonic(KeyEvent.VK_S);
		this.add(menu);
		
		item = new JMenuItem();
		item.setText("NUOVO");
		item.setMnemonic(KeyEvent.VK_N);
		item.addActionListener(this);
		menu.add(item);
				  
		item = new JMenuItem();
		item.setText("APRI");
		item.setMnemonic(KeyEvent.VK_A);
		item.addActionListener(this);
		menu.add(item);
		
		item = new JMenuItem();
		item.setText("SALVA");
		item.setMnemonic(KeyEvent.VK_S);
		item.addActionListener(this);
		menu.add(item);
		
		menu.addSeparator();
		
		item = new JMenuItem();
		item.setText("STAMPA");
		item.setMnemonic(KeyEvent.VK_P);
		item.addActionListener(this);
		menu.add(item);
		
		menu.addSeparator();
		
		item = new JMenuItem();
		item.setText("ESCI");
		item.setMnemonic(KeyEvent.VK_N);
		item.addActionListener(this);
		menu.add(item);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JMenuItem item = (JMenuItem) e.getSource();
		System.out.println(item.getText());
		
		String menu = item.getText();
		/*********************
		 * MENU FILE
		 *********************/
		if(menu.equals("MS ACCESS")) 	openMsAccess();
		if(menu.equals("MYSQL")) 		openMySQL();
		if(menu.equals("NUOVO")) 		return;
		if(menu.equals("APRI")) 		return;
		if(menu.equals("IMPORTA")) 		openXML();
		if(menu.equals("SALVA")) 		return;
		if(menu.equals("STAMPA")) 		return;
		if(menu.equals("ESCI"))			{P2DWindow.getInstance().closeWindow(); mySQLSelector.getInstance().dispose();}
		/*********************
		 * MENU VISTA
		 *********************/
		if(menu.equals("RESET"))
		{
			P2DCanvas.getInstance().getCamera().setViewScale(1.0f);
			ZoomSlider.getInstance().setValue(100);
		}
		
	}
	
	private void openXML()
	{
		JFileChooser fc = new JFileChooser();
		fc.addChoosableFileFilter(new XMLFileFilter());
		fc.setAcceptAllFileFilterUsed(false);
		fc.showDialog(this, "Apri");
		File f = fc.getSelectedFile();
		xml = new ReadXML(f);
	}

	private void openMsAccess()
	{
		JFileChooser fc = new JFileChooser();
		fc.setFileFilter(new MDBFileFilter());
		fc.setDialogTitle("Seleziona l'archivio Ms Access da convertire");
		fc.setDialogType(JFileChooser.OPEN_DIALOG);
		fc.setDoubleBuffered(true);
		fc.setMultiSelectionEnabled(false);
		fc.showOpenDialog(P2DWindow.getInstance());
		
		File f = fc.getSelectedFile();
		MsAccessConnection.getInstance().setDbPath(f.getAbsolutePath());
		MsAccessConnection.getInstance().getConnection();
		DatabaseStructure dbStruct = MsAccessConnection.getInstance().getStructure();
		
		XMLWriter xml = new XMLWriter(dbStruct);
		try {
			f = new File("d:\\testXML.xml");
			xml.create();
			xml.write(f);
			
			ReadXML readXML = new ReadXML(f);
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	private void openMySQL()
	{
		mySQLSelector request = mySQLSelector.getInstance();
		Point p = new Point((int) P2DWindow.getInstance().getBounds().getCenterX(),
				            (int) P2DWindow.getInstance().getBounds().getCenterY());
		request.setLocation(p);
		request.setVisible(true);
	}
}
